home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / awksrc.zip / IOP.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  8KB  |  316 lines

  1. /*
  2.  * iop.c - do i/o related things.
  3.  */
  4.  
  5. /* 
  6.  * Copyright (C) 1986, 1988, 1989, 1991, 1992 the Free Software Foundation, Inc.
  7.  * 
  8.  * This file is part of GAWK, the GNU implementation of the
  9.  * AWK Progamming Language.
  10.  * 
  11.  * GAWK is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  * 
  16.  * GAWK is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  * 
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with GAWK; see the file COPYING.  If not, write to
  23.  * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26. #include "awk.h"
  27.  
  28. #ifndef atarist
  29. #define INVALID_HANDLE (-1)
  30. #else
  31. #include <stddef.h>
  32. #include <fcntl.h>
  33. #define INVALID_HANDLE  (__SMALLEST_VALID_HANDLE - 1)
  34. #endif  /* atarist */
  35.  
  36.  
  37. #ifdef TEST
  38. int bufsize = 8192;
  39.  
  40. void
  41. fatal(s)
  42. char *s;
  43. {
  44.     printf("%s\n", s);
  45.     exit(1);
  46. }
  47. #endif
  48.  
  49. int
  50. optimal_bufsize(fd)
  51. int fd;
  52. {
  53.     struct stat stb;
  54.  
  55. #ifdef VMS
  56.     /*
  57.      * These values correspond with the RMS multi-block count used by
  58.      * vms_open() in vms/vms_misc.c.
  59.      */
  60.     if (isatty(fd) > 0)
  61.         return BUFSIZ;
  62.     else if (fstat(fd, &stb) < 0)
  63.         return 8*512;    /* conservative in case of DECnet access */
  64.     else
  65.         return 24*512;
  66.  
  67. #else
  68.     /*
  69.      * System V doesn't have the file system block size in the
  70.      * stat structure. So we have to make some sort of reasonable
  71.      * guess. We use stdio's BUFSIZ, since that is what it was
  72.      * meant for in the first place.
  73.      */
  74. #ifdef BLKSIZE_MISSING
  75. #define    DEFBLKSIZE    BUFSIZ
  76. #else
  77. #define DEFBLKSIZE    (stb.st_blksize ? stb.st_blksize : BUFSIZ)
  78. #endif
  79.  
  80. /* #ifdef TEST
  81.     return bufsize;
  82. #else
  83. */
  84. /*
  85. #ifndef atarist
  86.     if (isatty(fd))
  87. #else
  88. */
  89.     /*
  90.      * On ST redirected stdin does not have a name attached
  91.      * (this could be hard to do to) and fstat would fail
  92.      */
  93.     if (0 == fd || isatty(fd))
  94. /* #endif  atarist */
  95.         return BUFSIZ;
  96. #ifndef BLKSIZE_MISSING
  97.     /* VMS POSIX 1.0: st_blksize is never assigned a value, so zero it */
  98.     stb.st_blksize = 0;
  99. #endif
  100.     if (fstat(fd, &stb) == -1)
  101.         fatal("can't stat fd %d (%s)", fd, strerror(errno));
  102.     if (lseek(fd, (off_t)0, 0) == -1)
  103.         return DEFBLKSIZE;
  104.     return ((int) (stb.st_size < DEFBLKSIZE ? stb.st_size : DEFBLKSIZE));
  105. // #endif    /*! TEST */
  106. #endif    /*! VMS */
  107. }
  108.  
  109. IOBUF *
  110. iop_alloc(fd)
  111. int fd;
  112. {
  113.     IOBUF *iop;
  114.  
  115.     if (fd == INVALID_HANDLE)
  116.         return NULL;
  117.     emalloc(iop, IOBUF *, sizeof(IOBUF), "iop_alloc");
  118.     iop->flag = 0;
  119.     #ifdef TEST
  120.     fprintf(stderr, "determining input handle. FD: %d\n", fd);
  121.     #endif
  122.  
  123.     if (isatty(fd)) {
  124.     #ifdef TEST
  125.     fprintf(stderr, "Determined that it's a tty. FD: %d\n", fd);
  126.     #endif
  127.         iop->flag |= IOP_IS_TTY;
  128.     }
  129.     iop->size = optimal_bufsize(fd);
  130.     iop->secsiz = -2;
  131.     errno = 0;
  132.     iop->fd = fd;
  133.     iop->off = iop->buf = NULL;
  134.     iop->cnt = 0;
  135.     return iop;
  136. }
  137.  
  138. /*
  139.  * Get the next record.  Uses a "split buffer" where the latter part is
  140.  * the normal read buffer and the head part is an "overflow" area that is used
  141.  * when a record spans the end of the normal buffer, in which case the first
  142.  * part of the record is copied into the overflow area just before the
  143.  * normal buffer.  Thus, the eventual full record can be returned as a
  144.  * contiguous area of memory with a minimum of copying.  The overflow area
  145.  * is expanded as needed, so that records are unlimited in length.
  146.  * We also mark both the end of the buffer and the end of the read() with
  147.  * a sentinel character (the current record separator) so that the inside
  148.  * loop can run as a single test.
  149.  */
  150. int
  151. get_a_record(out, iop, grRS)
  152. char **out;
  153. IOBUF *iop;
  154. register int grRS;
  155. {
  156.     register char *bp = iop->off;
  157.     char *bufend;
  158.     char *start = iop->off;            /* beginning of record */
  159.     int saw_newline;
  160.     char rs;
  161.     int eat_whitespace;
  162.  
  163.     if (iop->cnt == EOF)    /* previous read hit EOF */
  164.         return EOF;
  165.  
  166.     if (grRS == 0) {    /* special case:  grRS == "" */
  167.         rs = '\n';
  168.         eat_whitespace = 0;
  169.         saw_newline = 0;
  170.     } else
  171.         rs = (char) grRS;
  172.  
  173.     /* set up sentinel */
  174.     if (iop->buf) {
  175.         bufend = iop->buf + iop->size + iop->secsiz;
  176.         *bufend = rs;
  177.     } else
  178.         bufend = NULL;
  179.  
  180.     for (;;) {    /* break on end of record, read error or EOF */
  181.  
  182.         /* Following code is entered on the first call of this routine
  183.          * for a new iop, or when we scan to the end of the buffer.
  184.          * In the latter case, we copy the current partial record to
  185.          * the space preceding the normal read buffer.  If necessary,
  186.          * we expand this space.  This is done so that we can return
  187.          * the record as a contiguous area of memory.
  188.          */
  189.         if (bp >= bufend) {
  190.             char *oldbuf = NULL;
  191.             char *oldsplit = iop->buf + iop->secsiz;
  192.             long len;    /* record length so far */
  193.  
  194.             len = bp - start;
  195.             if (len > iop->secsiz) {
  196.                 /* expand secondary buffer */
  197.                 if (iop->secsiz == -2)
  198.                     iop->secsiz = 256;
  199.                 while (len > iop->secsiz)
  200.                     iop->secsiz *= 2;
  201.                 oldbuf = iop->buf;
  202.                 emalloc(iop->buf, char *,
  203.                     iop->size+iop->secsiz+2, "get_a_record");
  204.                 bufend = iop->buf + iop->size + iop->secsiz;
  205.                 *bufend = rs;
  206.             }
  207.             if (len > 0) {
  208.                 char *newsplit = iop->buf + iop->secsiz;
  209.  
  210.                 if (start < oldsplit) {
  211.                     memcpy(newsplit - len, start,
  212.                             oldsplit - start);
  213.                     memcpy(newsplit - (bp - oldsplit),
  214.                             oldsplit, bp - oldsplit);
  215.                 } else
  216.                     memcpy(newsplit - len, start, len);
  217.             }
  218.             bp = iop->end = iop->off = iop->buf + iop->secsiz;
  219.             start = bp - len;
  220.             if (oldbuf) {
  221.                 free(oldbuf);
  222.                 oldbuf = NULL;
  223.             }
  224.         }
  225.         /* Following code is entered whenever we have no more data to
  226.          * scan.  In most cases this will read into the beginning of
  227.          * the main buffer, but in some cases (terminal, pipe etc.)
  228.          * we may be doing smallish reads into more advanced positions.
  229.          */
  230.         if (bp >= iop->end) {
  231.             iop->cnt = read(iop->fd, iop->end, bufend - iop->end);
  232.             if (iop->cnt == -1)
  233.                 fatal("error reading input: %s", strerror(errno));
  234.             else if (iop->cnt == 0) {
  235.                 iop->cnt = EOF;
  236.                 break;
  237.             }
  238.             iop->end += iop->cnt;
  239.             *iop->end = rs;
  240.         }
  241.         if (grRS == 0) {
  242.         #ifdef TEST
  243.             int default_FS = 0;
  244.         #else
  245.             extern int default_FS;
  246.         #endif
  247.  
  248.             if (default_FS && (bp == start || eat_whitespace)) {
  249.                 while (bp < iop->end && isspace(*bp))
  250.                     bp++;
  251.                 if (bp == iop->end) {
  252.                     eat_whitespace = 1;
  253.                     continue;
  254.                 } else
  255.                     eat_whitespace = 0;
  256.             }
  257.             if (saw_newline && *bp == rs) {
  258.                 bp++;
  259.                 break;
  260.             }
  261.             saw_newline = 0;
  262.         }
  263.  
  264.         while (*bp++ != rs)
  265.             ;
  266.  
  267.         if (bp <= iop->end) {
  268.             if (grRS == 0)
  269.                 saw_newline = 1;
  270.             else
  271.                 break;
  272.         } else
  273.             bp--;
  274.     }
  275.     if (iop->cnt == EOF && start == bp)
  276.         return EOF;
  277.  
  278.     iop->off = bp;
  279.     if (*--bp == rs)
  280.         *bp = '\0';
  281.     else
  282.         bp++;
  283.     if (grRS == 0) {
  284.         if (*--bp == rs)
  285.             *bp = '\0';
  286.         else
  287.             bp++;
  288.     }
  289.  
  290.     *out = start;
  291.     return bp - start;
  292. }
  293.  
  294. #ifdef TEST
  295. main(argc, argv)
  296. int argc;
  297. char *argv[];
  298. {
  299.     IOBUF *iop;
  300.     char *out;
  301.     int cnt;
  302.     char rs[2];
  303.  
  304.     rs[0] = 0;
  305.     if (argc > 1)
  306.         bufsize = atoi(argv[1]);
  307.     if (argc > 2)
  308.         rs[0] = *argv[2];
  309.     iop = iop_alloc(0);
  310.     while ((cnt = get_a_record(&out, iop, rs[0])) > 0) {
  311.         fwrite(out, 1, cnt, stdout);
  312.         fwrite(rs, 1, 1, stdout);
  313.     }
  314. }
  315. #endif
  316.